﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using HefaSyncTool.Controls;
using System.Diagnostics;
using HefaSyncTool.SyncManager;
using SyncCommon;
using SyncCommon.DAL;
using System.Threading;

namespace HefaSyncTool
{
    public partial class AccountSetting : HefaForm
    {
        private int Count = 0;
        private int EleHeight = 130;
        private int EleWidth = 487;
        public delegate void ShowLoadingHandler();
        public AccountSetting()
            :base()
        {
            InitializeComponent();
            InitalData();
        }

        public void InitalData()
        {
            if (SharedVal.WebSites != null)
            {
                foreach (KeyValuePair<long, SDWebsite> pair in SharedVal.WebSites)
                {
                    AddLoginControl(pair.Key, pair.Value.UserName, pair.Value.Password, pair.Value.LoginLogo, pair.Value.IsValid, pair.Value.IsStart);
                }
                this.Height = EleHeight * ((SharedVal.WebSites.Count -1) / 2 + 1) + 30;
                this.Width = 980;
            }
        }

        public void AddLoginControl(long webit,string username,string password,string icon, bool isSucc,bool isStart)
        { 
            LoginAccountSet set = new LoginAccountSet(webit,username,password,icon,isSucc,isStart);
            set.OpenRegister += new LoginAccountSet.OpenRegisterHanlder(set_OpenRegister);
            set.CheckLogin += new LoginAccountSet.CheckLoginHanlder(set_CheckLogin);
            set.StopWork += new LoginAccountSet.StopWorkHandler(set_StopWork);
            this.PanelContainer.Controls.Add(set);
            set.Left = (Count % 2) * EleWidth;
            set.Top = (Count / 2) * EleHeight;
            Count++;
        }

        void set_StopWork(LoginAccountSet sender, bool stopOrstart)
        {
            SharedVal.WebSites[sender.WebID].IsStart = stopOrstart;
            WebLogin login = WebLogin.SingleOrDefault("WHERE WebSiteID=@0", sender.WebID);

            if (login != null)
            {
                login.IsStart = stopOrstart;
                login.Save();
            }
        }

        public void CheckAcountCorrect()
        {
            CheckAccount account = new CheckAccount();
            account.ShowDialog();

        }

        void set_CheckLogin(LoginAccountSet sender, string username, string password)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            CheckAccount account = new CheckAccount();
            Thread t = new Thread(delegate() {
                account.ShowDialog();
            });
            t.Start();
            try
            {
                if (SyncCenter.CheckLogin(sender.WebID, username, password))
                {
                    account.Close();
                    Control.CheckForIllegalCrossThreadCalls = true;
                    HFMessageBox.ShowTip("校验成功！", "成功");
                    WebLogin login = WebLogin.SingleOrDefault("WHERE WebSiteID=@0", sender.WebID);

                    if (login == null)
                    {
                        login = new WebLogin();
                        login.WebSiteName = SharedVal.WebSites[sender.WebID].Name;
                        login.WebSiteID = sender.WebID;
                    }

                    login.UserName = username;
                    login.Password = EncryptCenter.DESEncrypt(password);
                    login.IsValid = true;
                    login.IsStart = true;
                    login.Save();
                    SDWebsite web = SharedVal.WebSites[sender.WebID];
                    web.IsValid = true;
                    web.IsStart = true;
                    web.UserName = username;
                    web.Password = password;
                    sender.Success = true;
                    sender.IsStart = true;
                }
                else
                {
                    account.Close();
                    Control.CheckForIllegalCrossThreadCalls = true;
                    HFMessageBox.ShowError("登录失败。请检查帐号状态和密码是否正确！");
                    WebLogin login = WebLogin.SingleOrDefault("WHERE WebSiteID=@0", sender.WebID);

                    if (login != null)
                    {
                        login.UserName = username;
                        login.Password = EncryptCenter.DESEncrypt(password);
                        login.IsValid = false;
                        login.IsStart = false;
                        login.Save();
                    }
                    SDWebsite web = SharedVal.WebSites[sender.WebID];

                    web.UserName = username;
                    web.Password = password;
                    web.IsValid = false;
                    web.IsStart = false;
                    sender.ClearPassword();
                    sender.Success = false;
                    sender.IsStart = false;
                }
            }
            catch (Exception exp)
            {
                LogCenter.Log(exp);
            }
           
        }

        private void set_OpenRegister(LoginAccountSet sender)
        {
            Process p = new Process();
            p.StartInfo = new ProcessStartInfo(SharedVal.WebSites[sender.WebID].RegisterURL);
            p.Start();
        }
    }
}
